******************************************************
******** This file creates the file County_pscs_xxxx.dta  ********
**** input file:  $dir3\county_supply_xxx.dta  (created in $dir1\create_county_supply_curves.do)
**** input file:  $dir3\PriceVectors.csv  (contains price vector from National34.m) (saved as price.dta)
**** input files: $dir3\CSPSVectors.csv (contains CS PS vector from National34.m) (saved as cspsagg.dta)
**** input files: $dir4/MuniSourceDecoder.dta  (locates muni sources in counties)
**** input files: $dir4\County_pop.dta  (population by county)

clear matrix
drop _all
set mem 2g

***** user toggles:  sph is 1;  jeh is 2; ck is 3
global user=2    
****  insheet toggles:  $toggle==1 (insheets) $toggle==0 (doesn't insheet)
global toggle=1
****  supply toggles:  $supplytoggle==2 (corrects trapezoids) $supplytoggle==1 (shifts supply out) $supplytoggle==0 (no correction) 
global supplytoggle=2

global retail_tax_adj=0.63
***********  Note:  verify that run=="run2" is first run and run=="run28" is last run 

***sph's directories
if $user==1{
global dir1="C:\AllStephen\My Dropbox\Jon n Chris\1 - Supply Curves\"
global dir2="C:\AllStephen\My Dropbox\Jon n Chris\2 - Policy Simulation\Simulation Results"
global dir3="C:\AllStephen\My Dropbox\Jon n Chris\3 - Dist of Gains\"
global dir4="C:\AllStephen\My Dropbox\Jon n Chris\4 - Land Area Calcs\"
global dir5="C:\AllStephen\My Dropbox\Jon n Chris\5 - Probits\Data\"
}

***jeh's directories
if $user==2 {
global dir1="/Users/Jon/Dropbox/LandUse/1 - Supply Curves/"
global dir2="/Users/Jon/Dropbox/LandUse/2 - Policy Simulation/Simulation Results/"
global dir3="/Users/Jon/Dropbox/LandUse/3 - Dist of Gains/"
global dir4="/Users/Jon/Dropbox/LandUse/4 - Land Area Calcs/"
global dir5="/Users/Jon/Dropbox/LandUse/5 - Probits/Data/"
}


*** Make a loop to prepare data for all the scenarios except endog corn and elastic gas supply ***
foreach S in highP lowP highiLUC WastePass OldCorn LessElastic MoreElastic{

*********************************
*** prepare the price data
*********************************
*** price.dta is created from National34.m using the following insheet
if $toggle==1 {
cd "$dir2"

*insheet using "PriceVectors.csv", clear
*insheet using "PriceVectors_ck.csv", clear
*insheet using "PriceVectors_org.csv", clear
*insheet using "PriceVectors_old.csv", clear
insheet using "PriceVectors_`S'.csv", clear

save price.dta, replace
}
use price.dta, clear
rename v1 price_BAU
rename v2 price_RFS 
rename v3 price_CT
rename v4 price_LCFS
rename v5 price_SUBS
*
generate str ethtype = "corn" in 1 
replace ethtype="agres" in 2
replace ethtype="orch" in 3
replace ethtype="forest" in 4
replace ethtype="msw" in 5
replace ethtype="msw_food" in 6
replace ethtype="hec" in 7
cd "$dir3"
save temp2.dta, replace


*********************************
*** prepare the CS PS data
*********************************
*** cspsagg.dta is created from National34.m using the following insheet
if $toggle==1 {
cd "$dir2"


*insheet using "CSPSVectors.csv", clear
*insheet using "CSPSVectors_ck.csv", clear
*insheet using "CSPSVectors_org.csv", clear
*insheet using "CSPSVectors_old.csv", clear
insheet using "CSPSVectors_`S'.csv", clear

save cspsagg.dta, replace
}
use cspsagg.dta, clear
rename v1 BAU
rename v2 RFS 
rename v3 CT
rename v4 LCFS
rename v5 SUBS
*
generate str surplus = "CS" in 1 
replace surplus="corn" in 2
replace surplus="agres" in 3
replace surplus="orch" in 4
replace surplus="forest" in 5
replace surplus="msw" in 6
replace surplus="msw_food" in 7
replace surplus="hec" in 8
replace surplus="gas" in 9
replace surplus="trans" in 10
*
foreach X in RFS CT LCFS  SUBS{
global delCS_`X'=`X'[1]
}
global CTrev=CT[10]
global SUBSrev=SUBS[10]
*
cd "$dir3"
save temp3.dta, replace



*********************************
**** Calculate surplus for each county 
*********************************
cd "$dir3"
use county_supply_new_$supplytoggle.dta, clear

***BAU surplus
merge m:1 ethtype using temp2.dta, nogen
*
foreach X in BAU RFS CT LCFS  SUBS{
foreach Y in hec corn pulpwood {
g acres_`Y'_`X'=acres_`Y'[_n-1] + (acres_`Y'-acres_`Y'[_n-1])* (price_`X'-price[_n-1])/(price-price[_n-1]) if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace acres_`Y'_`X'= acres_`Y' if price_`X' >price & run =="run28"
replace acres_`Y'_`X'= max(price_`X'/price*acres_`Y',0) if price_`X' <price & run =="run2"
}
*
g quant_tons_`X'=quant_tons[_n-1] + (quant_tons-quant_tons[_n-1])* (price_`X'-price[_n-1])/(price-price[_n-1]) if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace quant_tons_`X'= quant_tons if price_`X' >price & run =="run28"
replace quant_tons_`X'= max(price_`X'/price*quant_tons,0) if price_`X' <price & run =="run2"
*
g eth_`X'=ethanol[_n-1] + (ethanol-ethanol[_n-1])* (price_`X'-price[_n-1])/(price-price[_n-1]) if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace eth_`X'= ethanol if price_`X' >price & run =="run28"
replace eth_`X'= max(price_`X'/price*ethanol,0) if price_`X' <price & run =="run2"
*
g cost_`X'=cost[_n-1] + 0.5*(price_`X' + price[_n-1])*(eth_`X'-ethanol[_n-1])  if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace cost_`X'=cost if price_`X' >price & run =="run28"
replace cost_`X'= 0.5*($retail_tax_adj+price_`X')*max(price_`X'/price*ethanol,0) if price_`X' <price & run =="run2"
*
g ps_`X'=price_`X'*eth_`X'-cost_`X'
}
**** need some additional work to get the weights from quant_tons  (quant_tons not used)
collapse  acres_hec_* acres_corn_* acres_pulpwood_* ps_BAU ps_RFS ps_CT ps_LCFS ps_SUBS, by (ethtype source_id)
*
g length=length(source_id)
gen str_fips = substr(source_id,2,7)
*Handle Municipal Sources 
cd "$dir1"
merge m:1 str_fips using "MuniSourceDecoder.dta"
drop if _merge == 2
replace str_fips = str_fips2 if _merge == 3
drop state county name fips55 fips stfips str_fips2 fips_l
drop _merge
gen state = real(substr(str_fips,1,2)) if length==8
gen county = real(substr(str_fips,3,3)) if length==8
replace state=real(substr(source_id,2,2)) if length==6
replace county=real(substr(source_id,4,3)) if length==6
*
collapse (sum) acres_* ps_BAU ps_RFS ps_CT ps_LCFS ps_SUBS, by (state county)
*
foreach X in  RFS CT LCFS  SUBS{
g dps_`X'=ps_`X'-ps_BAU
}
keep state county dps* acres_*
cd "$dir3"
merge 1:1 state county using "County_pop.dta"
drop if county==0
g population=real(census2000pop)
drop str_* Q* popes* county_length state_length census2000pop estimatesbase2000 _merge 
drop if population==.
preserve
collapse (sum) population
global poptot=population[1]
restore
foreach X in  RFS CT LCFS  SUBS{
replace dps_`X'=0 if dps_`X'==.
}
g dcsps_RFS=dps_RFS +$delCS_RFS*population/$poptot
g dcsps_CT=dps_CT +$delCS_CT*population/$poptot + $CTrev*population/$poptot
g dcsps_LCFS=dps_LCFS +$delCS_LCFS*population/$poptot
g dcsps_SUBS=dps_SUBS +$delCS_SUBS*population/$poptot -$SUBSrev*population/$poptot
preserve
collapse (sum) dps*
display "RFS    "  dps_RFS[1]+$delCS_RFS
display "CT     "  dps_CT[1] + $delCS_CT + $CTrev
display "LCFS   " dps_LCFS[1] +$delCS_LCFS
display "SUBS   " dps_SUBS[1] +$delCS_SUBS -$SUBSrev
restore
keep state county population dcsps_* acres_*
cd "$dir5"
*save county_csps.dta, replace
save county_csps_`S'_$supplytoggle.dta, replace

}

*********************************************
*** Now do endogenous corn price scenario ***
*********************************************
*********************************
*** prepare the price data
*********************************
*** price.dta is created from National34.m using the following insheet
if $toggle==1 {
cd "$dir2"

*insheet using "PriceVectors.csv", clear
*insheet using "PriceVectors_ck.csv", clear
*insheet using "PriceVectors_org.csv", clear
*insheet using "PriceVectors_old.csv", clear
insheet using "PriceVectors_CornElast.csv", clear

save price.dta, replace
}
use price.dta, clear
rename v1 price_BAU
rename v2 price_RFS 
rename v3 price_CT
rename v4 price_LCFS
rename v5 price_SUBS
*
generate str ethtype = "corn" in 1 
replace ethtype="agres" in 2
replace ethtype="orch" in 3
replace ethtype="forest" in 4
replace ethtype="msw" in 5
replace ethtype="msw_food" in 6
replace ethtype="hec" in 7
cd "$dir3"
save temp2.dta, replace


*********************************
*** prepare the CS PS data
*********************************
*** cspsagg.dta is created from National34.m using the following insheet
if $toggle==1 {
cd "$dir2"


*insheet using "CSPSVectors.csv", clear
*insheet using "CSPSVectors_ck.csv", clear
*insheet using "CSPSVectors_org.csv", clear
*insheet using "CSPSVectors_old.csv", clear
insheet using "CSPSVectors_CornElast.csv", clear

save cspsagg.dta, replace
}
use cspsagg.dta, clear
rename v1 BAU
rename v2 RFS 
rename v3 CT
rename v4 LCFS
rename v5 SUBS
*
generate str surplus = "CS" in 1 
replace surplus="corn" in 2
replace surplus="agres" in 3
replace surplus="orch" in 4
replace surplus="forest" in 5
replace surplus="msw" in 6
replace surplus="msw_food" in 7
replace surplus="hec" in 8
replace surplus="gas" in 9
replace surplus="trans" in 10
*
foreach X in RFS CT LCFS  SUBS{
global delCS_`X'=`X'[1]
}
global CTrev=CT[10]
global SUBSrev=SUBS[10]
*
cd "$dir3"
save temp3.dta, replace



*********************************
**** Calculate surplus for each county 
*********************************
cd "$dir3"
use county_supply_CornElast_$supplytoggle.dta, clear

***BAU surplus
merge m:1 ethtype using temp2.dta, nogen
*
foreach X in BAU RFS CT LCFS  SUBS{
foreach Y in hec corn pulpwood {
g acres_`Y'_`X'=acres_`Y'[_n-1] + (acres_`Y'-acres_`Y'[_n-1])* (price_`X'-price[_n-1])/(price-price[_n-1]) if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace acres_`Y'_`X'= acres_`Y' if price_`X' >price & run =="run28"
replace acres_`Y'_`X'= max(price_`X'/price*acres_`Y',0) if price_`X' <price & run =="run2"
}
*
g quant_tons_`X'=quant_tons[_n-1] + (quant_tons-quant_tons[_n-1])* (price_`X'-price[_n-1])/(price-price[_n-1]) if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace quant_tons_`X'= quant_tons if price_`X' >price & run =="run28"
replace quant_tons_`X'= max(price_`X'/price*quant_tons,0) if price_`X' <price & run =="run2"
*
g eth_`X'=ethanol[_n-1] + (ethanol-ethanol[_n-1])* (price_`X'-price[_n-1])/(price-price[_n-1]) if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace eth_`X'= ethanol if price_`X' >price & run =="run28"
replace eth_`X'= max(price_`X'/price*ethanol,0) if price_`X' <price & run =="run2"
*
g cost_`X'=cost[_n-1] + 0.5*(price_`X' + price[_n-1])*(eth_`X'-ethanol[_n-1])  if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace cost_`X'=cost if price_`X' >price & run =="run28"
replace cost_`X'= 0.5*($retail_tax_adj+price_`X')*max(price_`X'/price*ethanol,0) if price_`X' <price & run =="run2"
*
g ps_`X'=price_`X'*eth_`X'-cost_`X'
}
**** need some additional work to get the weights from quant_tons  (quant_tons not used)
collapse  acres_hec_* acres_corn_* acres_pulpwood_* ps_BAU ps_RFS ps_CT ps_LCFS ps_SUBS, by (ethtype source_id)
*
g length=length(source_id)
gen str_fips = substr(source_id,2,7)
*Handle Municipal Sources 
cd "$dir1"
merge m:1 str_fips using "MuniSourceDecoder.dta"
drop if _merge == 2
replace str_fips = str_fips2 if _merge == 3
drop state county name fips55 fips stfips str_fips2 fips_l
drop _merge
gen state = real(substr(str_fips,1,2)) if length==8
gen county = real(substr(str_fips,3,3)) if length==8
replace state=real(substr(source_id,2,2)) if length==6
replace county=real(substr(source_id,4,3)) if length==6
*
collapse (sum) acres_* ps_BAU ps_RFS ps_CT ps_LCFS ps_SUBS, by (state county)
*
foreach X in  RFS CT LCFS  SUBS{
g dps_`X'=ps_`X'-ps_BAU
}
keep state county dps* acres_*
cd "$dir3"
merge 1:1 state county using "County_pop.dta"
drop if county==0
g population=real(census2000pop)
drop str_* Q* popes* county_length state_length census2000pop estimatesbase2000 _merge 
drop if population==.
preserve
collapse (sum) population
global poptot=population[1]
restore
foreach X in  RFS CT LCFS  SUBS{
replace dps_`X'=0 if dps_`X'==.
}
g dcsps_RFS=dps_RFS +$delCS_RFS*population/$poptot
g dcsps_CT=dps_CT +$delCS_CT*population/$poptot + $CTrev*population/$poptot
g dcsps_LCFS=dps_LCFS +$delCS_LCFS*population/$poptot
g dcsps_SUBS=dps_SUBS +$delCS_SUBS*population/$poptot -$SUBSrev*population/$poptot
preserve
collapse (sum) dps*
display "RFS    "  dps_RFS[1]+$delCS_RFS
display "CT     "  dps_CT[1] + $delCS_CT + $CTrev
display "LCFS   " dps_LCFS[1] +$delCS_LCFS
display "SUBS   " dps_SUBS[1] +$delCS_SUBS -$SUBSrev
restore
keep state county population dcsps_* acres_*
cd "$dir5"
*save county_csps.dta, replace
save county_csps_CornElast_$supplytoggle.dta, replace

*************************************************
*** Now do upward sloped gas supply scenarios ***
*************************************************
foreach S in GasElast1 GasElast2{

*********************************
*** prepare the price and PS data
*********************************
*** price.dta is created from National34.m using the following insheet
if $toggle==1 {
cd "$dir2"

* Prepare data for change in gasoline producer's surplus
insheet using insheet using "PSGasVector_`S'.csv", clear
xpose, clear
rename v1 delPSgasRFS
rename v2 delPSgasLCFS
rename v3 delPSgasCT
rename v4 delPSgasSUBS 
cd "$dir3"
save "dPSGas_`S'.dta", replace


*insheet using "PriceVectors.csv", clear
*insheet using "PriceVectors_ck.csv", clear
*insheet using "PriceVectors_org.csv", clear
*insheet using "PriceVectors_old.csv", clear
cd "$dir2"
insheet using "PriceVectors_`S'.csv", clear

save price.dta, replace
}
use price.dta, clear
rename v1 price_BAU
rename v2 price_RFS 
rename v3 price_CT
rename v4 price_LCFS
rename v5 price_SUBS
*
generate str ethtype = "corn" in 1 
replace ethtype="agres" in 2
replace ethtype="orch" in 3
replace ethtype="forest" in 4
replace ethtype="msw" in 5
replace ethtype="msw_food" in 6
replace ethtype="hec" in 7
cd "$dir3"
save temp2.dta, replace


*********************************
*** prepare the CS PS data
*********************************
*** cspsagg.dta is created from National34.m using the following insheet
if $toggle==1 {
cd "$dir2"


*insheet using "CSPSVectors.csv", clear
*insheet using "CSPSVectors_ck.csv", clear
*insheet using "CSPSVectors_org.csv", clear
*insheet using "CSPSVectors_old.csv", clear
insheet using "CSPSVectors_`S'.csv", clear

save cspsagg.dta, replace
}
use cspsagg.dta, clear
rename v1 BAU
rename v2 RFS 
rename v3 CT
rename v4 LCFS
rename v5 SUBS
*
generate str surplus = "CS" in 1 
replace surplus="corn" in 2
replace surplus="agres" in 3
replace surplus="orch" in 4
replace surplus="forest" in 5
replace surplus="msw" in 6
replace surplus="msw_food" in 7
replace surplus="hec" in 8
replace surplus="gas" in 9
replace surplus="trans" in 10
*
foreach X in RFS CT LCFS  SUBS{
global delCS_`X'=`X'[1]
}
global CTrev=CT[10]
global SUBSrev=SUBS[10]
*
cd "$dir3"
save temp3.dta, replace



*********************************
**** Calculate surplus for each county 
*********************************
cd "$dir3"
use county_supply_new_$supplytoggle.dta, clear

***BAU surplus
merge m:1 ethtype using temp2.dta, nogen
*
foreach X in BAU RFS CT LCFS  SUBS{
foreach Y in hec corn pulpwood {
g acres_`Y'_`X'=acres_`Y'[_n-1] + (acres_`Y'-acres_`Y'[_n-1])* (price_`X'-price[_n-1])/(price-price[_n-1]) if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace acres_`Y'_`X'= acres_`Y' if price_`X' >price & run =="run28"
replace acres_`Y'_`X'= max(price_`X'/price*acres_`Y',0) if price_`X' <price & run =="run2"
}
*
g quant_tons_`X'=quant_tons[_n-1] + (quant_tons-quant_tons[_n-1])* (price_`X'-price[_n-1])/(price-price[_n-1]) if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace quant_tons_`X'= quant_tons if price_`X' >price & run =="run28"
replace quant_tons_`X'= max(price_`X'/price*quant_tons,0) if price_`X' <price & run =="run2"
*
g eth_`X'=ethanol[_n-1] + (ethanol-ethanol[_n-1])* (price_`X'-price[_n-1])/(price-price[_n-1]) if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace eth_`X'= ethanol if price_`X' >price & run =="run28"
replace eth_`X'= max(price_`X'/price*ethanol,0) if price_`X' <price & run =="run2"
*
g cost_`X'=cost[_n-1] + 0.5*(price_`X' + price[_n-1])*(eth_`X'-ethanol[_n-1])  if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace cost_`X'=cost if price_`X' >price & run =="run28"
replace cost_`X'= 0.5*($retail_tax_adj+price_`X')*max(price_`X'/price*ethanol,0) if price_`X' <price & run =="run2"
*
g ps_`X'=price_`X'*eth_`X'-cost_`X'
}
**** need some additional work to get the weights from quant_tons  (quant_tons not used)
collapse  acres_hec_* acres_corn_* acres_pulpwood_* ps_BAU ps_RFS ps_CT ps_LCFS ps_SUBS, by (ethtype source_id)
*
g length=length(source_id)
gen str_fips = substr(source_id,2,7)
*Handle Municipal Sources 
cd "$dir1"
merge m:1 str_fips using "MuniSourceDecoder.dta"
drop if _merge == 2
replace str_fips = str_fips2 if _merge == 3
drop state county name fips55 fips stfips str_fips2 fips_l
drop _merge
gen state = real(substr(str_fips,1,2)) if length==8
gen county = real(substr(str_fips,3,3)) if length==8
replace state=real(substr(source_id,2,2)) if length==6
replace county=real(substr(source_id,4,3)) if length==6
*
collapse (sum) acres_* ps_BAU ps_RFS ps_CT ps_LCFS ps_SUBS, by (state county)
*
foreach X in  RFS CT LCFS  SUBS{
g dps_`X'=ps_`X'-ps_BAU
}
keep state county dps* acres_*
cd "$dir3"
merge 1:1 state county using "County_pop.dta"
drop if county==0
g population=real(census2000pop)
drop str_* Q* popes* county_length state_length census2000pop estimatesbase2000 _merge 
drop if population==.
preserve
collapse (sum) population
global poptot=population[1]
restore
foreach X in  RFS CT LCFS  SUBS{
replace dps_`X'=0 if dps_`X'==.
}

cd "$dir3"
*append using "dPSGas_`S'.dta"
append using "dPSGas_GasElast1.dta"
foreach P in RFS LCFS CT SUBS {
egen temp_`P' = max(delPSgas`P')
replace delPSgas`P' = temp_`P'
}
drop temp_*
drop if state == .

g dcsps_RFS=dps_RFS + delPSgasRFS*population/$poptot + $delCS_RFS*population/$poptot
g dcsps_CT=dps_CT + delPSgasCT*population/$poptot +$delCS_CT*population/$poptot + $CTrev*population/$poptot
g dcsps_LCFS=dps_LCFS + delPSgasLCFS*population/$poptot +$delCS_LCFS*population/$poptot
g dcsps_SUBS=dps_SUBS + delPSgasSUBS*population/$poptot +$delCS_SUBS*population/$poptot -$SUBSrev*population/$poptot
preserve
collapse (sum) dps*
display "RFS    "  dps_RFS[1]+$delCS_RFS
display "CT     "  dps_CT[1] + $delCS_CT + $CTrev
display "LCFS   " dps_LCFS[1] +$delCS_LCFS
display "SUBS   " dps_SUBS[1] +$delCS_SUBS -$SUBSrev
restore
keep state county population dcsps_* acres_*
cd "$dir5"
*save county_csps.dta, replace
save county_csps_`S'_$supplytoggle.dta, replace

}

*************************************************
*** Now do upward sloped gas supply scenarios ***
***	ASSUME DPS GAS ALLOCATED BY REF CAPACITY  ***
*************************************************
foreach S in GasElast1 GasElast2{

*********************************
*** prepare the price and PS data
*********************************
*** price.dta is created from National34.m using the following insheet
if $toggle==1 {
cd "$dir2"

* Prepare data for change in gasoline producer's surplus
insheet using insheet using "PSGasVector_`S'.csv", clear
xpose, clear
rename v1 delPSgasRFS
rename v2 delPSgasLCFS
rename v3 delPSgasCT
rename v4 delPSgasSUBS 
cd "$dir3"
save "dPSGas_`S'.dta", replace

*insheet using "PriceVectors.csv", clear
*insheet using "PriceVectors_ck.csv", clear
*insheet using "PriceVectors_org.csv", clear
*insheet using "PriceVectors_old.csv", clear
cd "$dir2"
insheet using "PriceVectors_`S'.csv", clear

save price.dta, replace
}
use price.dta, clear
rename v1 price_BAU
rename v2 price_RFS 
rename v3 price_CT
rename v4 price_LCFS
rename v5 price_SUBS
*
generate str ethtype = "corn" in 1 
replace ethtype="agres" in 2
replace ethtype="orch" in 3
replace ethtype="forest" in 4
replace ethtype="msw" in 5
replace ethtype="msw_food" in 6
replace ethtype="hec" in 7
cd "$dir3"
save temp2.dta, replace


*********************************
*** prepare the CS PS data
*********************************
*** cspsagg.dta is created from National34.m using the following insheet
if $toggle==1 {
cd "$dir2"


*insheet using "CSPSVectors.csv", clear
*insheet using "CSPSVectors_ck.csv", clear
*insheet using "CSPSVectors_org.csv", clear
*insheet using "CSPSVectors_old.csv", clear
insheet using "CSPSVectors_`S'.csv", clear

save cspsagg.dta, replace
}
use cspsagg.dta, clear
rename v1 BAU
rename v2 RFS 
rename v3 CT
rename v4 LCFS
rename v5 SUBS
*
generate str surplus = "CS" in 1 
replace surplus="corn" in 2
replace surplus="agres" in 3
replace surplus="orch" in 4
replace surplus="forest" in 5
replace surplus="msw" in 6
replace surplus="msw_food" in 7
replace surplus="hec" in 8
replace surplus="gas" in 9
replace surplus="trans" in 10
*
foreach X in RFS CT LCFS  SUBS{
global delCS_`X'=`X'[1]
}
global CTrev=CT[10]
global SUBSrev=SUBS[10]
*
cd "$dir3"
save temp3.dta, replace



*********************************
**** Calculate surplus for each county 
*********************************
cd "$dir3"
use county_supply_new_$supplytoggle.dta, clear

***BAU surplus
merge m:1 ethtype using temp2.dta, nogen
*
foreach X in BAU RFS CT LCFS  SUBS{
foreach Y in hec corn pulpwood {
g acres_`Y'_`X'=acres_`Y'[_n-1] + (acres_`Y'-acres_`Y'[_n-1])* (price_`X'-price[_n-1])/(price-price[_n-1]) if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace acres_`Y'_`X'= acres_`Y' if price_`X' >price & run =="run28"
replace acres_`Y'_`X'= max(price_`X'/price*acres_`Y',0) if price_`X' <price & run =="run2"
}
*
g quant_tons_`X'=quant_tons[_n-1] + (quant_tons-quant_tons[_n-1])* (price_`X'-price[_n-1])/(price-price[_n-1]) if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace quant_tons_`X'= quant_tons if price_`X' >price & run =="run28"
replace quant_tons_`X'= max(price_`X'/price*quant_tons,0) if price_`X' <price & run =="run2"
*
g eth_`X'=ethanol[_n-1] + (ethanol-ethanol[_n-1])* (price_`X'-price[_n-1])/(price-price[_n-1]) if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace eth_`X'= ethanol if price_`X' >price & run =="run28"
replace eth_`X'= max(price_`X'/price*ethanol,0) if price_`X' <price & run =="run2"
*
g cost_`X'=cost[_n-1] + 0.5*(price_`X' + price[_n-1])*(eth_`X'-ethanol[_n-1])  if price >price_`X' & price[_n-1] <= price_`X'[_n-1] & source_id ==source_id[_n-1]
replace cost_`X'=cost if price_`X' >price & run =="run28"
replace cost_`X'= 0.5*($retail_tax_adj+price_`X')*max(price_`X'/price*ethanol,0) if price_`X' <price & run =="run2"
*
g ps_`X'=price_`X'*eth_`X'-cost_`X'
}
**** need some additional work to get the weights from quant_tons  (quant_tons not used)
collapse  acres_hec_* acres_corn_* acres_pulpwood_* ps_BAU ps_RFS ps_CT ps_LCFS ps_SUBS, by (ethtype source_id)
*
g length=length(source_id)
gen str_fips = substr(source_id,2,7)
*Handle Municipal Sources 
cd "$dir1"
merge m:1 str_fips using "MuniSourceDecoder.dta"
drop if _merge == 2
replace str_fips = str_fips2 if _merge == 3
drop state county name fips55 fips stfips str_fips2 fips_l
drop _merge
gen state = real(substr(str_fips,1,2)) if length==8
gen county = real(substr(str_fips,3,3)) if length==8
replace state=real(substr(source_id,2,2)) if length==6
replace county=real(substr(source_id,4,3)) if length==6
*
collapse (sum) acres_* ps_BAU ps_RFS ps_CT ps_LCFS ps_SUBS, by (state county)
*
foreach X in  RFS CT LCFS  SUBS{
g dps_`X'=ps_`X'-ps_BAU
}
keep state county dps* acres_*
cd "$dir3"
merge 1:1 state county using "County_pop.dta"
drop if county==0
g population=real(census2000pop)
drop str_* Q* popes* county_length state_length census2000pop estimatesbase2000 _merge 
drop if population==.
preserve
collapse (sum) population
global poptot=population[1]
restore
foreach X in  RFS CT LCFS  SUBS{
replace dps_`X'=0 if dps_`X'==.
}

cd "$dir3"
*append using "dPSGas_`S'.dta"
append using "dPSGas_GasElast1.dta"
foreach P in RFS LCFS CT SUBS {
egen temp_`P' = max(delPSgas`P')
replace delPSgas`P' = temp_`P'
}
drop temp_*
drop if state == .

g dcsps_RFS=dps_RFS + $delCS_RFS*population/$poptot
g dcsps_CT=dps_CT +$delCS_CT*population/$poptot + $CTrev*population/$poptot
g dcsps_LCFS=dps_LCFS +$delCS_LCFS*population/$poptot
g dcsps_SUBS=dps_SUBS +$delCS_SUBS*population/$poptot -$SUBSrev*population/$poptot
preserve
collapse (sum) dps*
display "RFS    "  dps_RFS[1]+$delCS_RFS
display "CT     "  dps_CT[1] + $delCS_CT + $CTrev
display "LCFS   " dps_LCFS[1] +$delCS_LCFS
display "SUBS   " dps_SUBS[1] +$delCS_SUBS -$SUBSrev
restore
keep state county population dcsps_* acres_*
cd "$dir5"
*save county_csps.dta, replace
save county_csps_`S'REFCAP_$supplytoggle.dta, replace

}


